<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh' ? '520px':'660px'" @close='close'>
    <el-form ref="form" :model="record" :label-width="lang === 'zh'? '130px':'245px'" :rules="rules" label-position="left" style="margin:0 30px;">
      <p style="color:#ff4949;">{{$t('注：管理口带宽限制过小可能会影响页面访问')}}</p>
      <el-form-item :label="$t('物理接口')" prop="name">
        <el-select v-model="record.name" :placeholder="$t('请选择出接口')" ref="recordName" :disabled='record.id !== null' clearable filterable size='small' style="width:267px;">
          <el-option v-for="item in policyList" :disabled="item.enPortMode === 3 || item.enPortMode === 2" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('出方向限制带宽')" prop="limit">
        <el-input v-emoji v-model="record.limit" ref="limit" style="width:267px;" size='small'>
          <el-select slot="append" v-model="record.limit_unit" ref="limitUnit" filterable size='small' style="width:100px;">
            <el-option v-for="item in guaranteeLimitList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('入方向限制带宽')" prop="limit_ingress">
        <el-input v-emoji v-model="record.limit_ingress" ref="limitIngress" style="width:267px;" size='small'>
          <el-select slot="append" v-model="record.limit_unit_ingress" ref="limitUnitIngress" filterable size='small' style="width:100px;">
            <el-option v-for="item in guaranteeLimitList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getBandWidthArray, addInterface, editInterface } from '@/api/network/bandwidth'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'NeighborDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      record: {
        id: null,
        name: '',
        limit: '',
        limit_unit: 'Kbps',
        limit_ingress: '',
        limit_unit_ingress: 'Kbps'
      },
      recordCopy: {
        id: null,
        name: '',
        limit: '',
        limit_unit: 'Kbps',
        limit_ingress: '',
        limit_unit_ingress: 'Kbps'
      },
      policyList: [],
      guaranteeLimitList: ['bps', 'Kbps', 'Mbps'],
      rules: {
        limit_ingress: [{ required: false, validator: validateName, trigger: 'blur' }],
        limit: [{ required: false, validator: validateName, trigger: 'blur' }],
        name: [{ required: false, validator: validateName, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const inputData = ['limit', 'limitIngress']
        const selectData = ['recordName', 'limitUnit', 'limitUnitIngress']
        inputClass(this.$refs, inputData)
        selectClass(this.$refs, selectData)
      })
      if (data) {
        // this.record = Object.assign(this.record, data)
        Object.keys(this.record).forEach((key) => {
          this.record[key] = data[key]
        })
        this.record.old_name = data.name
      } else {
        Promise.all([
          getPhysicalApiArray(),
          getBandWidthArray()
        ]).then(res => {
          /* 你的逻辑代码 */
          if (res[0].status === 0 && res[1].status === 0) {
            for (let policy of res[0].info) {
              if (!res[1].info.find(item => item.name === policy.strPhysicsName)) {
                this.policyList.push(policy)
              }
            }
          }
        })
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.record.id) {
            addInterface(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            editInterface(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.policyList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less">
.el-input-group__append {
  background-color: #ffffff;
}
</style>
